using System;
using System.Collections.Generic;
using System.Text;
using Portal.Core.DAL;
using System.Data;
using System.ComponentModel;

namespace ChannelVN.CoreBO.Threads
{
	public class Threaddetails
	{
		public Threaddetails() { }
		#region GetThreadDetails Store
		[DataObjectMethod(DataObjectMethodType.Select)]
		public DataTable GetThreadDetails(string strWhere, int PageSize, int StartRow)
		{
			DataTable objresult;
			//lay thong tin ve menh de where de loc thong tin
			if (strWhere == null)
				strWhere = "";
			int intPageNum = StartRow / PageSize + 1;
			//lay duoc du lieu cua tat ca nhung thang co trang thai la status can lay
			using (MainDB objdb = new MainDB())
			{
				//objresult = objdb.ThreadDetailsCollection.GetPageAsDataTable(intPageNum, PageSize, strWhere, " Threaddetails_ID DESC"); ;
				objresult = objdb.StoredProcedures.vc_ThreadDetails_SelectList(strWhere, StartRow, PageSize);
			}
			return objresult;
		}
		public DataTable GetThreadDetails_Old(string strWhere, int PageSize, int StartRow)
		{
			DataTable objresult;
			//lay thong tin ve menh de where de loc thong tin
			if (strWhere == null)
				strWhere = "";
			int intPageNum = StartRow / PageSize + 1;
			//lay duoc du lieu cua tat ca nhung thang co trang thai la status can lay
			using (MainDB objdb = new MainDB())
			{
				objresult = objdb.ThreadDetailsCollection.GetPageAsDataTable(intPageNum, PageSize, strWhere, " Threaddetails_ID DESC"); ;
			}
			return objresult;
		}
		#endregion
		/// <summary>
		/// Ham thuc hien lay thong tin row count de phan trang
		/// </summary>
		/// <param name="strWhere"></param>
		/// <returns></returns>
		/// 
		#region GetThreadRowsCount
		public int GetThreadRowsCount(string strWhere)
		{
			if (strWhere == null)
				strWhere = "";
			DataTable objresult;
			using (MainDB objdb = new MainDB())
			{
				//objresult = objdb.ThreadDetailsCollection.GetAsDataTable(strWhere, ""); ;
				objresult = objdb.StoredProcedures.vc_ThreadDetails_SelectList_Count(strWhere);
			}
			if (objresult.Rows.Count == 0) return 0;
			else return Convert.ToInt32(objresult.Rows[0][0]);
			//return objresult.Rows.Count;
		}
		public int GetThreadRowsCount_Old(string strWhere)
		{
			if (strWhere == null)
				strWhere = "";
			DataTable objresult;
			using (MainDB objdb = new MainDB())
			{
				objresult = objdb.ThreadDetailsCollection.GetAsDataTable(strWhere, ""); ;
			}
			return objresult.Rows.Count;
		}
		#endregion
		/// <summary>
		/// Ham dung de cap nhat tin vao trong thread
		/// </summary>
		/// <param name="_selected_id"></param>
		/// <param name="threadID"></param>
		[DataObjectMethod(DataObjectMethodType.Insert)]
		public void AddnewsThread(string _selected_id, int threadID)
		{
			if (_selected_id == null) { return; }
			if (_selected_id == "") { return; }
			string[] objTempSel = _selected_id.Split(',');
			MainDB objDB = new MainDB();
			ThreadDetailRow objrow = new ThreadDetailRow();
			DataTable checkExit;
			foreach (string temp in objTempSel)
			{
				checkExit = objDB.StoredProcedures.vc_ThreadDetail_CheckExistingNews_ID_Thread_ID(temp, threadID);
				if (checkExit.Rows.Count == 0)
				{
					objrow.Thread_ID = threadID;
					objrow.News_ID = temp.Trim();
					objDB.ThreadDetailCollection.Insert(objrow);
				}
			}
		}

		public void AddnewsThread(string _selected_id, int threadID, MainDB objDB)
		{
			if (_selected_id == null) { return; }
			if (_selected_id == "") { return; }
			string[] objTempSel = _selected_id.Split(',');

			ThreadDetailRow objrow = new ThreadDetailRow();
			DataTable checkExit;
			foreach (string temp in objTempSel)
			{
				checkExit = objDB.StoredProcedures.vc_ThreadDetail_CheckExistingNews_ID_Thread_ID(temp, threadID);
				if (checkExit.Rows.Count == 0)
				{
					objrow.Thread_ID = threadID;
					objrow.News_ID = temp.Trim();
					objDB.ThreadDetailCollection.Insert(objrow);
				}
			}
		}

		[DataObjectMethod(DataObjectMethodType.Delete)]
		public void DelNewsThread(string _selected_id)
		{
			if (_selected_id.IndexOf(",") <= 0)
			{
				//DeleteFile(_selected_id);
				try
				{
					int intid = Convert.ToInt32(_selected_id);
					using (MainDB objDB = new MainDB())
					{

						objDB.ThreadDetailCollection.DeleteByPrimaryKey(intid);

					}
				}
				catch { }
			}
			else
			{
				string[] objTempSel = _selected_id.Split(',');
				//foreach (string temp in objTempSel)
				//{
				//    DeleteFile(temp.Trim());
				//}
				try
				{
					using (MainDB objDB = new MainDB())
					{
						objDB.ThreadDetailCollection.Delete(" Threaddetails_ID in (" + _selected_id + ")");
					}
				}
				catch { }
			}
		}

		public void DeleteThreadByNewsId(string news_id)
		{
			using (MainDB objdb = new MainDB())
			{
				objdb.SelectQuery("Delete ThreadDetail Where News_ID = '" + news_id + "'");
			}
		}

		public DataTable GetThreadByNewsId(string strNews_Id)
		{
			using (MainDB objdb = new MainDB())
			{
				return objdb.StoredProcedures.ThreadDetail_GetThreadDetailByNewsId(strNews_Id);
			}
		}
	}
}
